package com.emex.app.stuff.provider;

import com.emex.app.shared.data.User;
import com.emex.app.shared.ws.AuthenticationWebService;
import com.emex.app.stuff.utils.WebServiceHelper;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;

/**
 *
 * @author Sergey Karavaev
 */
public class AuthenticationDataProvider extends Service {

    private AuthenticationWebService service = null;

    private static AuthenticationDataProvider instance = null;

    /**
     * Constructor
     */
    private AuthenticationDataProvider() throws MalformedURLException {
        super(new URL(WebServiceHelper.getDefault().getWebServiceURL("web", "Authentication")),
                WebServiceHelper.getDefault().getWebServiceQName("Authentication"));
        service = getPort(AuthenticationWebService.class);
    }

    /**
     * Get instance of data provider.
     *
     * @return data provider instance
     */
    public static synchronized AuthenticationDataProvider getInstance() {
        if (instance == null) {
            new RequestWebService() {
                @Override
                protected void doRequestBody() throws MalformedURLException {
                    instance = new AuthenticationDataProvider();
                }
            }.doRequest();
        }
        return instance;
    }

    /**
     * Simple authenticate
     *
     * @return
     */
    public User authenticate(String username, String password) {
        return service.authenticate(username, password);
    }

    public BindingProvider getBindingProvider() {
        return (BindingProvider) service;
    }

    public int deleteUserByPrimaryKey(Integer id) {
        return service.deleteUserByPrimaryKey(id);
    }

    public User insertUser(User record) {
        return service.insertUser(record);
    }

    public List<User> getAllUsers() {
        return service.getAllUsers();
    }

    public User updateUserByPrimaryKey(User record) {
        return service.updateUserByPrimaryKey(record);
    }

    public User selectUserByPrimaryKey(Integer id) {
        return service.selectUserByPrimaryKey(id);
    }
}
